/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-13
 * Time: 下午10:01
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.system.userrole.UserRoleController', {
    extend:'Ext.app.Controller',
    models:['system.user.UserManageModel','system.role.RoleManageModel'],
    stores:['system.user.UserManageStore','system.role.RoleManageStore'],
    views:['system.userrole.UserRoleView'],
    refs:[
        {ref:'userRoleView',selector:'userRoleView'},
        {ref:'userRoleLeft',selector:'userRoleLeft'},
        {ref:'userRoleRight',selector:'userRoleRight'}
    ],
    init:function(){
        var controller = this;
        this.control({
            'userRoleLeft button[action=find]':{
                click:function(){
                     controller.getUserRoleLeft().getStore().loadPage(1);
                }
            },
            'userRoleLeft':{
                afterrender:function(){
                    var store = this.getUserRoleLeft().getStore();
                    var userNo = this.getUserRoleLeft().down('textfield[name=userNo]');
                    var userName = this.getUserRoleLeft().down('textfield[name=userName]');
                    var status = this.getUserRoleLeft().down('combo[name=status]');
                    store.on({
                        beforeload:function(){
                            controller.record = null;
                            store.proxy.extraParams = {
                                userNo:userNo.getValue(),
                                userName:userName.getValue(),
                                status:status.getValue()
                            }
                        }
                    })
                },
                selectionchange: function(model, records) {
                    if (records.length > 0) {
                        controller.record = records[0];
                        callAjaxLocal( basePath+'system/userrole/queryRoleCheckedList',{userId:records[0].get('userId')},function(result){
                            var store = controller.getUserRoleRight().getStore();
                            var selected = [];
                            for(var i = 0;i< result.length;i++){
                                for(var j = 0 ; j < store.getCount() ; j ++ ){
                                    if(result[i] == store.getAt(j).get('roleId')){
                                        selected.push(store.getAt(j));
                                        break;
                                    }
                                }
                            }
                            controller.getUserRoleRight().getSelectionModel().select(selected);
                        });
                    }
                }
            },
            'userRoleRight button[action=save]' :{
                 click:function(){
                     var re = controller.record;
                     if(re == null){
                         Ext.MessageBox.show({title:'提示',msg:'请先选择用户数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                         return;
                     }
                     var records = controller.getUserRoleRight().getSelectionModel().getSelection();
                     var ids = [];
                     Ext.each(records,function (val){
                         ids.push(val.get('roleId'));
                     });
                     callAjax( basePath+'system/userrole/saveUserRole',{userId:re.get('userId'),ids:ids},function(result){

                     });
                 }
            }



        })
    }
});
